package com.snpic.appaw.module.third.service.bjpt;

import com.alibaba.fastjson.JSON;
import com.snpic.appaw.framework.common.pojo.CommonResult;
import com.snpic.appaw.module.third.api.bjpt.dto.*;
import com.snpic.appaw.module.third.config.BJPTConfig;
import com.snpic.appaw.module.third.service.httpclient.HttpClientServerImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import static com.snpic.appaw.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.snpic.appaw.module.third.enums.ErrorCodeConstants.HTTPCLIENT_REQUEST_FAILED;

/**
 * @author JiJun
 * @date 2025/9/16 18:22
 * @修改时间 2025/9/16 18:22
 * @description
 */
@Service
@Slf4j
public class BJPTInvocationServiceImpl implements BJPTInvocationService {
	@Resource
	private BJPTConfig bjptConfig;

	@Resource
	private HttpClientServerImpl httpClientServer;

	/**
	 * 车辆车型查询接口
	 *
	 * @param reqDTO
	 * @return
	 */
	@Override
	public CommonResult<CarModelInfoInquiryRespTotalDto> carModelInfoInquiry(CarModelInfoInquiryReqTotalDto reqDTO) {
		//1. 查教第三方接口地址
		String url = bjptConfig.getCarModelInfoInquiry().getUrl();

		// 2.请求头不需要修改
		//3. 调用第三方接口
		String postJson = null;
		try {
			postJson = httpClientServer.doPostJson(url, reqDTO, null, "报价平台", "车辆车型查询接口");
		} catch (HttpClientServerImpl.HttpClientException e) {
			log.error("第三方接口调用失败,报价平台,车辆车型查询接口", e);
			throw exception(HTTPCLIENT_REQUEST_FAILED);
		}
		//4. 解析返回结果
		CarModelInfoInquiryRespTotalDto respDTO = JSON.parseObject(postJson, CarModelInfoInquiryRespTotalDto.class);
		return CommonResult.success(respDTO);
	}

	/**
	 * 历史保单查询接口
	 *
	 * @param reqDTO
	 * @return
	 */
	@Override
	public CommonResult<QueryHisPolicyDetailRespTotalDto> queryHisPolicyDetail(QueryHisPolicyDetailReqTotalDto reqDTO) {
		//1. 查教第三方接口地址
		String url = bjptConfig.getQueryHisPolicyDetail().getUrl();
		// 2.请求头不需要修改
		//3. 调用第三方接口
		String postJson = null;
		try {
			postJson = httpClientServer.doPostJson(url, reqDTO, null, "报价平台", "历史保单查询接口");
		} catch (HttpClientServerImpl.HttpClientException e) {
			log.error("第三方接口调用失败,报价平台,历史保单查询接口", e);
			throw exception(HTTPCLIENT_REQUEST_FAILED);
		}

		//4. 解析返回结果
		QueryHisPolicyDetailRespTotalDto respDTO = JSON.parseObject(postJson, QueryHisPolicyDetailRespTotalDto.class);
		return CommonResult.success(respDTO);
	}
}
